/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.ContentChangeListener;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.TreeNodeChangeListener;
import han.bifang.gwt.client.common.TreeNodeContentListener;
import han.bifang.gwt.client.common.entity.MappingService;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.content.AppContent;
import han.bifang.gwt.client.service.content.CreateServiceBasicContent;
import han.bifang.gwt.client.service.content.CreateServiceBusBindingContent;
import han.bifang.gwt.client.service.content.CreateServiceConfigContent;
import han.bifang.gwt.client.service.content.ImportRemoteServiceBusBindingContent;
import han.bifang.gwt.client.service.content.ImportRemoteServiceConfigContent;
import han.bifang.gwt.client.service.content.ImportRemoteServiceContent;
import han.bifang.gwt.client.service.content.RemoteDomainContent;
import han.bifang.gwt.client.service.content.RemoteServiceRootContent;
import han.bifang.gwt.client.service.content.RemoveMappingServiceContent;
import han.bifang.gwt.client.service.content.ServiceRootContent;
import han.bifang.gwt.client.service.content.ServiceRootMgmtContent;
import han.bifang.gwt.client.service.content.ServiceSearchable;
import han.bifang.gwt.client.service.tree.ServiceDomainTree;
import han.bifang.gwt.client.service.tree.ServiceDomainTreeItem;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class ServiceTab extends Composite implements TreeNodeContentListener,
		ContentChangeListener {
	private TreeItem selectedItem = null;

	private DecoratorPanel panel = null;

	private ServiceMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private TreeNodeChangeListener listener = null;

	private HorizontalPanel hp;

	private DecoratorPanel treePanel;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
			}

			ArrayList ds = (ArrayList) res.getResponse();
			context.put(ServiceConstants.APP_LIST, ds.get(1));
			context.put(ServiceConstants.DOMAIN_LIST, ds.get(0));
			context.put(ServiceConstants.BUS_LIST, ds.get(2));

			initTree();

			ServiceRootMgmtContent drc = new ServiceRootMgmtContent(cons,
					service, context);
			setContentChangeListener(drc);
			// drc.setWidth("100%");
			// drc.setContentChangeListener(this);
			panel.setWidget(drc);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
			}

			ArrayList ds = (ArrayList) res.getResponse();
			context.put(ServiceConstants.APP_LIST, ds.get(1));
			context.put(ServiceConstants.DOMAIN_LIST, ds.get(0));
			context.put(ServiceConstants.BUS_LIST, ds.get(2));

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	private void setContentChangeListener(Content t) {
		t.setContentChangeListener(this);
	}

	public void setTreeNodeChangeListener(TreeNodeChangeListener l) {

		this.listener = l;
	}

	public void onTreeNodeSelected(TreeItem item) {
		this.selectedItem = item;
		ServiceDomainTreeItem i = (ServiceDomainTreeItem) item;
		if (i.getSubType().equals(ServiceConstants.SERVICE_DOMAIN_ROOT)) {
			context.remove(ServiceConstants.APP_ID);
			ServiceRootMgmtContent src = new ServiceRootMgmtContent(cons,
					service, context);
			src.setContentChangeListener(this);
			this.panel.setWidget(src);
			return;
		}
		if (i.getSubType().equals(EntityConstants.TREE_APP_APP)) {
			AppContent src = new AppContent(cons, service, context, i.getKey());
			src.setContentChangeListener(this);
			this.panel.setWidget(src);
			context.put(ServiceConstants.APP_ID, i.getKey());
			return;
		}
		if (i.getSubType().equals(EntityConstants.TREE_DOMAIN_REMOTE)) {
			context.remove(ServiceConstants.APP_ID);
			RemoteDomainContent src = new RemoteDomainContent(cons, service,
					context, i.getKey());
			src.setContentChangeListener(this);
			this.panel.setWidget(src);

			return;
		}

	}

	public ServiceTab(BifangGWTServiceAsync service, GwtContext context) {
		this.service = service;
		this.context = context;

		cons = GWT.create(ServiceMessages.class);

		hp = new HorizontalPanel();
		hp.setWidth("100%");

		// hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		initWidget(hp);

	}

	private void initTree() {

		ServiceDomainTree tree = new ServiceDomainTree(cons, service, context);
		tree.setTreeNodeContentListener(this);
		// this.setTreeNodeChangeListener(tree);

		treePanel.setWidget(tree);
	}

	public void onLoad() {
		treePanel = new DecoratorPanel();
		treePanel.setWidth("100%");

		hp.add(treePanel);

		panel = new DecoratorPanel();
		panel.setWidth("100%");
		hp.add(panel);
		hp.setCellWidth(treePanel, "25%");
		hp.setCellWidth(panel, "75%");

		// RequestObject req = new RequestObject();
		// req.setAction("han.bifang.gwt.server.action.service.GetServiceInitAction");
		// service.call(req, cb);

	}

	public void init() {
		treePanel.setWidget(new HTML(""));
		panel.setWidget(new HTML(""));
		RequestObject req = new RequestObject();
		req
				.setAction("han.bifang.gwt.server.action.service.GetServiceTabInitAction");
		service.call(req, cb);

	}

	public void onContentChanged(String source, String dest, HashMap props) {
		if (source.equals(ServiceContentConstants.SERVICE_MGMT_ROOT)
				&& dest.equals(ServiceContentConstants.CREATE_SERVICE_BASIC)) {
			context.remove(ServiceConstants.SERVICE);
			CreateServiceBasicContent src = new CreateServiceBasicContent(cons,
					service, context);
			src.setContentChangeListener(this);
			
			context.put(ServiceConstants.SEARCH_PAGE, panel.getWidget());
			panel.setWidget(src);
			return;
		}
		if (source.equals(ServiceContentConstants.CREATE_SERVICE_BASIC)
				&& dest.equals(ServiceContentConstants.CREATE_SERVICE_CONFIG)) {

			CreateServiceConfigContent src = new CreateServiceConfigContent(
					cons, context);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		if (source.equals(ServiceContentConstants.CREATE_SERVICE_CONFIG)
				&& dest.equals(ServiceContentConstants.CREATE_SERVICE_BASIC)) {

			CreateServiceBasicContent src = new CreateServiceBasicContent(cons,
					service, context);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		if (source.equals(ServiceContentConstants.CREATE_SERVICE_CONFIG)
				&& dest
						.equals(ServiceContentConstants.CREATE_SERVICE_BUS_BINDING)) {

			CreateServiceBusBindingContent src = new CreateServiceBusBindingContent(
					cons, service, context);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		if (source.equals(ServiceContentConstants.CREATE_SERVICE_BUS_BINDING)
				&& dest.equals(ServiceContentConstants.CREATE_SERVICE_CONFIG)) {

			CreateServiceConfigContent src = new CreateServiceConfigContent(
					cons, context);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}

		if (source.equals(ServiceContentConstants.CREATE_SERVICE_BUS_BINDING)
				&& dest.equals(ServiceContentConstants.SERVICE_ROOT)) {
			String id = (String) props.get("id");
			ServiceRootContent src = new ServiceRootContent(cons, service,
					context, id);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		if (source.equals(ServiceContentConstants.SEARCH_RESULT)
				&& dest.equals(ServiceContentConstants.SERVICE_ROOT)) {
			String id = (String) props.get("id");
			ServiceRootContent src = new ServiceRootContent(cons, service,
					context, id);
			src.setContentChangeListener(this);
			context.put(ServiceConstants.SEARCH_PAGE, panel.getWidget());
			panel.setWidget(src);
			return;
		}

		if (source.equals(ServiceContentConstants.REMOVE_SERVICE)
				&& dest.equals(ServiceContentConstants.SEARCH_RESULT)) {
			context.remove(ServiceConstants.SERVICE);
			ServiceSearchable s = (ServiceSearchable) context
					.get(ServiceConstants.SEARCH_PAGE);
			s.refresh();
			panel.setWidget((Widget) s);

			return;
		}

		if (source.equals(ServiceContentConstants.REMOVE_SERVICE)
				&& dest.equals(ServiceContentConstants.SERVICE_ROOT)) {
			String id = (String) props.get("id");
			ServiceRootContent src = new ServiceRootContent(cons, service,
					context, id);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}

		if (source.equals(ServiceContentConstants.SERVICE_DETAIL)
				&& dest.equals(ServiceContentConstants.SEARCH_RESULT)) {
			context.remove(ServiceConstants.SERVICE);
			ServiceSearchable s = (ServiceSearchable) context
					.get(ServiceConstants.SEARCH_PAGE);
			if (s != null) {
				s.refresh();
				panel.setWidget((Widget) s);
			}else{
				
			}
			return;
		}

		if (source.equals(ServiceContentConstants.SERVICE_MGMT_ROOT)
				&& dest.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE)) {
			context.remove(ServiceConstants.MAPPING_SERVICE);
			context.remove(ServiceConstants.MAPPING_SERVICE_QOS);
			// context.remove(ServiceContentConstants.IMPORT_REMOTE_SERVICE_CONFIG);

			ImportRemoteServiceContent s = new ImportRemoteServiceContent(cons,
					service, context, (String) props.get("id"));
			s.setContentChangeListener(this);
			context.put(ServiceConstants.SEARCH_PAGE, panel.getWidget());
			
			panel.setWidget(s);
			
			
			return;
		}

		if (source.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_CONFIG)
				&& dest.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE)) {

			ImportRemoteServiceContent s = new ImportRemoteServiceContent(cons,
					service, context, (String) props.get("domainId"));
			s.setContentChangeListener(this);
			panel.setWidget(s);

			return;
		}

		if (source.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE)
				&& dest
						.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_CONFIG)) {

			ImportRemoteServiceConfigContent s = new ImportRemoteServiceConfigContent(
					cons, context);
			s.setContentChangeListener(this);
			panel.setWidget(s);

			return;
		}

		if (source.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_CONFIG)
				&& dest
						.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_BUS_BINDING)) {

			ImportRemoteServiceBusBindingContent s = new ImportRemoteServiceBusBindingContent(
					cons, service, context);
			s.setContentChangeListener(this);
			panel.setWidget(s);

			return;
		}

		if (source
				.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_BUS_BINDING)
				&& dest
						.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_CONFIG)) {

			ImportRemoteServiceConfigContent s = new ImportRemoteServiceConfigContent(
					cons, context);
			s.setContentChangeListener(this);
			panel.setWidget(s);

			return;
		}

		if (source
				.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_BUS_BINDING)
				&& dest.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)) {
			RemoteServiceRootContent src = new RemoteServiceRootContent(cons,
					service, context, (String) props.get("id"));
			src.setContentChangeListener(this);

			panel.setWidget(src);

			return;
		}

		if (source.equals(ServiceContentConstants.SEARCH_RESULT)
				&& dest.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)) {
			String id = (String) props.get("id");

			RemoteServiceRootContent src = new RemoteServiceRootContent(cons,
					service, context, id);
			src.setContentChangeListener(this);
			context.put(ServiceConstants.SEARCH_PAGE, panel.getWidget());
			panel.setWidget(src);
			return;
		}

		if (source.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)
				&& dest.equals(ServiceContentConstants.SEARCH_RESULT)) {
			ServiceSearchable s = (ServiceSearchable) context
					.get(ServiceConstants.SEARCH_PAGE);
			// Window.alert(s.getClass().getName());
			s.refreshMappingService();

			panel.setWidget((Widget) s);
			return;
		}

		if (source.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)
				&& dest.equals(ServiceContentConstants.REMOVE_REMOTE_SERVICE)) {

			RemoveMappingServiceContent s = new RemoveMappingServiceContent(
					cons, service, (MappingService) props.get("s"));
			s.setContentChangeListener(this);
			panel.setWidget(s);

			return;
		}

		if (source.equals(ServiceContentConstants.REMOVE_REMOTE_SERVICE)
				&& dest.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)) {

			RemoteServiceRootContent s = new RemoteServiceRootContent(cons,
					service, context, (String) props.get("id"));
			s.setContentChangeListener(this);
			panel.setWidget(s);

			return;
		}

		if (source.equals(ServiceContentConstants.REMOVE_REMOTE_SERVICE)
				&& dest.equals(ServiceContentConstants.REMOTE_DOMAIN_ROOT)) {
			ServiceSearchable ddc = (ServiceSearchable) context
					.get(ServiceConstants.SEARCH_PAGE);

			ddc.refreshMappingService();
			panel.setWidget((Widget) ddc);

			return;
		}

	}

}
